//
//  RecommendDetailViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/18.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "RecommendDetailViewController.h"
#import "RecommendDatailTableCell.h"
@interface RecommendDetailViewController ()



@end

@implementation RecommendDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.recommendTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20 - 44 - 49) style:UITableViewStylePlain];
    self.recommendTable.dataSource = self;
    self.recommendTable.delegate = self;
    [self.view addSubview:self.recommendTable];
    self.navigationItem.title = self.recommendScroll.name;
    self.dayAllArray = [NSMutableArray array];
    self.arrayDay = [NSMutableArray array];
    self.recommendDetail = [[RecommendDetail alloc] init];
    
    
    self.headView = [[RecommendHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.recommendTable.tableHeaderView = self.headView;
    
    [self getDataValue];
    //让菊花旋转起来
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    
    
    
}

-(void)getDataValue {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", self.recommendScroll.uId];
    NSLog(@"urlStr = %@", urlStr);
    [AFNGetDataValue getDataAFN:urlStr block:^(NSMutableDictionary *dataDic) {
        
        [self.recommendDetail setValuesForKeysWithDictionary:dataDic];
        self.recommendDetail.userName = [[dataDic objectForKey:@"user"] objectForKey:@"name"];
        self.recommendDetail.avatar_m = [[dataDic objectForKey:@"user"] objectForKey:@"avatar_m"];
        [self.headView setRecommendValue:self.recommendDetail];
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"days"]];
        for (NSMutableDictionary *dic in array) {
            self.recommendDetail = [[RecommendDetail alloc] init];
            self.recommendDetail.date = [dic objectForKey:@"date"];
            self.recommendDetail.day = [NSString stringWithFormat:@"%@", [dic objectForKey:@"day"]];

            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[dic objectForKey:@"waypoints"]];
            self.recommendDetail.dayArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in tempArray) {
                
                RecommendDetail *rec = [[RecommendDetail alloc] init];
                [rec setValuesForKeysWithDictionary:dic];
                rec.dayId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
                [self.dayAllArray addObject:rec];
                
                [self.arrayDay addObject:self.recommendDetail];
            }
        }

        [self.recommendTable reloadData];
        [self.HUD hide:YES];
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentViewController *com = [[CommentViewController alloc] init];
    RecommendDetail *rec = self.dayAllArray[indexPath.row];
    com.recDetail = rec;
    com.uId = self.recommendScroll.uId;
    com.title = self.recommendScroll.name;
    [self.navigationController pushViewController:com animated:YES];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return WIDTH;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    RecommendDatailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[RecommendDatailTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
//    cell.textLabel.text = @"1";
    RecommendDetail *rec = self.dayAllArray[indexPath.row];
    RecommendDetail *rec1 = self.arrayDay[indexPath.row];
    cell.recDetail = rec;
    cell.recDetail1 = rec1;

    return cell;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dayAllArray.count;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
